Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth: "", //窗口宽度
    windowHeigh: "", //窗口高度
    packetList: [{}], //红包队列
    packetNum: 200, //总共红包的数量
    showInter: '' // 循环动画定时器
  },
  onShow:function(){
 
  },
  onUnload:function(){
    this.innerAudioContext.stop()
    // this.innerAudioContext.destory()
  },
  onHide:function(){
    this.innerAudioContext.stop()
    this.innerAudioContext.destory()
  },

  onLoad: function(options) {
    let style=wx.getStorageSync('style')
    if(style!=''){
      this.setData({
        contentStyle:wx.getStorageSync('style')
      })
    }else{
      this.setData({
        contentStyle:'https://pic.imgdb.cn/item/6199ad6c2ab3f51d91e20c91.jpg'
      })
    }
    
    this.innerAudioContext = wx.createInnerAudioContext();
    this.innerAudioContext.src='https://wx.xxmtcds.top:2333/music/2021-11-30_9-38-19_66205.mp3'
    this.innerAudioContext.volume=0.7
    this.innerAudioContext.play()

  


    var that = this;

    // 获取手机屏幕宽高
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          windowWidth: res.windowWidth,
          windowHeigh: res.windowHeight,
          top: res.windowHeight - 100 //设置红包初始位置
        })
      }
    })

    //建立临时红包列表
    var packetList = [];
    //建立临时红包图片数组
    var srcList = ["https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171cd.png","https://pic.imgdb.cn/item/6199ab9b2ab3f51d91e171cd.png"];
      //生成初始化红包
      for (var i = 0; i < that.data.packetNum; i++) {
        // 生成随机位置（水平位置）
        var left = Math.random() * that.data.windowWidth - 40;
        // 优化位置，防止红包越界现象，保证每个红包都在屏幕之内
        if (left < 0) {
          left += 20;
        } else if (left > that.data.windowWidth) {
          left -= 20;
        }
        // 建立临时单个红包
        var packet = {
          src: srcList[Math.ceil(Math.random() * 2) - 1],
          id:i,
          top: -50,
          left: left,
          speed: Math.random() * 2500 + 3000 //生成随机掉落时间，保证每个掉落时间保持在3秒到5.5秒之间
        }
        // 将单个红包装入临时红包列表
        packetList.push(packet);
        // 将生成的临时红包列表更新至页面数据，页面内进行渲染
        that.setData({
          packetList: packetList
        })
      }

      // 初始化动画执行当前索引
      var tempIndex = 0;
      that.data.s=setTimeout(function(){

        wx.showModal({
          title: 'HAHAHA',
          content:'',
          cancelText: '退出',
          confirmText: '再来一局',
          success: function(res) {
            if(res.cancel){
            //这个跳转是左边按钮的跳转链接
              wx.navigateTo({
                url: '/pages/select/select'
              })
            }else{
            //这里是右边按钮的跳转链接
              wx.redirectTo({
                url: '/pages/game/game',
              })
            }
          }
        })
      
      },27000)
      // 开始定时器，每隔1秒掉落一次红包
      that.data.showInter = setInterval(function() {
        // 生成当前掉落红包的个数，1-3个
        var showNum = Math.ceil(Math.random() * 3);
        // 防止数组越界
        if (tempIndex * showNum >= that.data.packetNum) {
          // 如果所有预生成的红包已经掉落完，清除定时器
          clearInterval(that.data.showInter);

        
        } else {
          
          switch (showNum) {
            case 1:
              //设置临时红包列表当前索引下的top值，此处top值为动画运动的最终top值 
              packetList[tempIndex].top = that.data.windowHeigh;
              // 当前次掉落几个红包，索引值就加几
              tempIndex += 2;
              break;
            case 2:
              packetList[tempIndex].top = that.data.windowHeigh;
              packetList[tempIndex + 1].top = that.data.windowHeigh;
              tempIndex += 3;
              break;
            case 3:
              packetList[tempIndex].top = that.data.windowHeigh;
              packetList[tempIndex + 1].top = that.data.windowHeigh;
              packetList[tempIndex + 2].top = that.data.windowHeigh;
              tempIndex += 5;
              break;
            default:
              console.log();
          }
          // 更新红包列表数据
          that.setData({
            packetList: packetList
          })
        }
      }, 1000)
    
    },
    tapImages: function (event){
      this.innerAudioContext2 = wx.createInnerAudioContext();
      this.innerAudioContext2.src='https://wx.xxmtcds.top:2333/music/2021-11-29_18-57-38_47095.mp3'
      this.innerAudioContext2.volume=1
      this.innerAudioContext2.play()
      this.data.packetList[event.currentTarget.id].src=''

    }
  })